package t327.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import t327.mapper.WareHouseMapper;
import t327.pojo.*;
import t327.pojo.font.MaterialsAddWareHouseFont;
import t327.pojo.views.MaterialStoreView;
import t327.pojo.views.SubmitOrderStyleProductMaterialsView;
import t327.pojo.views.WarehouseStoreAreaBatchView;
import t327.service.iter.IWareHouseService;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class WareHouseServiceImpl implements IWareHouseService {
    @Autowired
    private WareHouseMapper wareHouseMapper;
    @Override
    public List<WarehouseStoreAreaBatchView> queryAllWarehouseStoreAreaBatchView() {
        return wareHouseMapper.queryAllWarehouseStoreAreaBatchView();
    }

    @Override
    public List<WarehouseStoreAreaBatchView> queryByName(String searchValue) {
        return wareHouseMapper.queryByName(searchValue);
    }

    @Override
    public List<WarehouseStoreAreaBatchView> getAllWarehouseStoreAreaBatchView() {
        return wareHouseMapper.getAllWarehouseStoreAreaBatchView();
    }

    @Override
    public List<Materials> queryAllMaterials() {
        return wareHouseMapper.queryAllMaterials();
    }


    @Override
    public List<StoreArea> queryAllStoreArea() {
        return wareHouseMapper.queryAllStoreArea();
    }

    @Override
    public List<Color> queryAllColor() {
        return wareHouseMapper.queryAllColor();
    }

    @Override
    public List<Warehouse> queryAllWarehouse() {
        return wareHouseMapper.queryAllWarehouse();
    }

    @Override
    public List<MaterialKind> queryAllMaterialKind() {
        return wareHouseMapper.queryAllMaterialKind();
    }

    @Override
    public List<Quality> queryAllQuality() {
        return wareHouseMapper.queryAllQuality();
    }

    @Override
    public HashMap<String,String> addMaterialsAndBatch(MaterialsAddWareHouseFont materialsAddWareHouseFont) {
        HashMap<String,String> map = new HashMap<>();
        Materials materials = new Materials();
        String materialsName = materialsAddWareHouseFont.getMaterialsName();
        Long materialsStatus = materialsAddWareHouseFont.getMaterialsStatus();
        String supplier = materialsAddWareHouseFont.getSupplier();
        materials.setMaterialsName(materialsName)
                .setMaterialsStatus(materialsStatus)
                .setSupplier(supplier);
        int i = wareHouseMapper.addMaterials(materials);
        if (i>0){
            Batch batch = new Batch();
            String batchNumber = materialsAddWareHouseFont.getBatchNumber();
            Long batchCount = materialsAddWareHouseFont.getBatchCount();
            if (batchCount < 0){
                map.put("code","206");
                map.put("msg","进货数量不能为负数");
                return map;
            }
            if (!isNumber(batchCount.toString())){
                map.put("code","206");
                map.put("msg","进货数量为数字");
                return map;
            }
            BigDecimal batchPrice = materialsAddWareHouseFont.getBatchPrice();
            if (batchPrice == null){
                map.put("code","206");
                map.put("msg","进货数量不能为空");
                return map;
            }
            if (batchPrice.compareTo(BigDecimal.ZERO) <= 0){
                map.put("code","206");
                map.put("msg","进货单价不能为负数");
                return map;
            }
            Date batchTime = materialsAddWareHouseFont.getBatchTime();
            batch.setBatchNumber(batchNumber)
                    .setBatchCount(batchCount)
                    .setBatchPrice(batchPrice)
                    .setBatchTime(batchTime);
            int i1 = wareHouseMapper.addBatch(batch);
            if (i1 > 0){
                Long colorId = materialsAddWareHouseFont.getColorId();
                Long materialsKindId = materialsAddWareHouseFont.getMaterialsKindId();
                Long qualityId = materialsAddWareHouseFont.getQualityId();
//        Long productDetailId = materialsAddWareHouseFont.getProductDetailId();
                int i2 = wareHouseMapper.addColorMaterialsRel(new ColorMaterialsRel(null, materials.getMaterialsId(), batch.getBatchNumberId(), colorId, materialsKindId, qualityId, null, null));
                if (i2>0){
                    Store store = new Store();
                    Date inputTime = materialsAddWareHouseFont.getInputTime();
                    store.setInputTime(inputTime);
                    store.setCountStatus(1l);
                    store.setProductionCount(batchCount);
                    int i3 = wareHouseMapper.addStore(store);
                    if (i3 > 0){
                        Long storeAreaId = materialsAddWareHouseFont.getStoreAreaId();
                        Long warehouseId = materialsAddWareHouseFont.getWarehouseId();
                        int i4 = wareHouseMapper.addStoreAreaWarehouseRel(new StoreAreaWarehouseRel(null, store.getStoreId(), storeAreaId, batch.getBatchNumberId(), warehouseId, null));
                        if (i4  > 0){
                            map.put("code","205");
                            map.put("msg","入库成功");
                            return map;
                        }else {
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            map.put("code","206");
                            map.put("msg","入库错误");
                            return map;
                        }

                    }else {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        map.put("code","206");
                        map.put("msg","入库错误");
                        return map;
                    }
                }else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    map.put("code","206");
                    map.put("msg","颜色错误");
                    return map;
                }
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                map.put("code","206");
                map.put("msg","批次错误");
                return map;
            }
        }else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code","206");
            map.put("msg","物料错误");
            return map;
        }
    }
    public static boolean isNumber(String str) {
        try {
            Long.parseLong(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    @Override
    @Transactional
    public int addMaterials(Materials materials) {
        try {
            // 在这里进行你的操作
            return wareHouseMapper.addMaterials(materials);
            // 如果有异常抛出，事务将回滚
        } catch (Exception e) {
            // 处理异常或者重新抛出异常
        }
        return 0;
    }

    @Override
    @Transactional
    public int addMaterialStoreRelchu(MaterialStoreRel materialStoreRel) {
        try {
            // 在这里进行你的操作
            return wareHouseMapper.addMaterialStoreRelchu(materialStoreRel);
            // 如果有异常抛出，事务将回滚
        } catch (Exception e) {
            // 处理异常或者重新抛出异常
        }
        return 0;
    }

    @Override
    @Transactional
    public int addStorechu(Store store) {
        try {
            // 在这里进行你的操作
            return wareHouseMapper.addStorechu(store);
            // 如果有异常抛出，事务将回滚
        } catch (Exception e) {
            // 处理异常或者重新抛出异常
        }
        return 0;
    }

    @Override
    public int addMaterialStoreRelRu(MaterialStoreRel materialStoreRel) {
        try {
            // 在这里进行你的操作
            return wareHouseMapper.addMaterialStoreRelRu(materialStoreRel);
            // 如果有异常抛出，事务将回滚
        } catch (Exception e) {
            // 处理异常或者重新抛出异常
        }
        return 0;
    }

    @Override
    public int addStoreRu(Store store) {
        try {
            // 在这里进行你的操作
            return wareHouseMapper.addStoreRu(store);
            // 如果有异常抛出，事务将回滚
        } catch (Exception e) {
            // 处理异常或者重新抛出异常
        }
        return 0;
    }

    @Override
    public List<Materials> getAllMaterials() {
        return wareHouseMapper.getAllMaterials();
    }

    @Override
    public List<SubmitOrderStyleProductMaterialsView> getSubMaterialViewAll() {
        return wareHouseMapper.getSubMaterialViewAll();
    }
    @Override
    public int updatelMaterials(Long id) {
        return wareHouseMapper.updatelMaterials(id);
    }

    @Override
    public int updatOrderAuditStatus(Long orderId) {
        return wareHouseMapper.updatOrderAuditStatus(orderId);
    }

    @Override
    public HashMap<String, String> addInventory(Inventory inventory) {
        HashMap<String,String> map = new HashMap<>();
        inventory.setProfitAndLoss(1l);
        Date date = new Date();
        //创建 Calendar 对象并设置为给定日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 添加三个月
        calendar.add(Calendar.MONTH, 3);
        // 获取三个月后的日期
        Date threeMonthsLater = calendar.getTime();
        inventory.setNextDate(threeMonthsLater);
        Long profitAndLossNum = inventory.getProfitAndLossNum();
        if (profitAndLossNum > 0){
            int i = wareHouseMapper.addInventory(inventory);
            if ( i  > 0){
                map.put("code","200");
                map.put("msg","盘点盈利"+profitAndLossNum);
                Long storeAreaWarehouseRelId = inventory.getStoreAreaWarehouseRelId();
                StoreAreaWarehouseRel storeAreaWarehouseRel = wareHouseMapper.queryStoreAreaWarehouseRel(storeAreaWarehouseRelId);
                Long storeId = storeAreaWarehouseRel.getStoreId();
                Store store = wareHouseMapper.queryStore(storeId);
                Long inventoryCount = inventory.getInventoryCount();
                store.setProductionCount(inventoryCount);
                int i1 = wareHouseMapper.updateStore(store);
                if (i1 < 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    map.put("code","205");
                    map.put("msg","仓库库存数量保存失败");
                    return map;
                }
                return map;
            }
        }else if (profitAndLossNum < 0){
            inventory.setProfitAndLoss(2l);
            int i = wareHouseMapper.addInventory(inventory);
            if(i > 0){
                map.put("code","200");
                map.put("msg","盘点盈亏"+profitAndLossNum);
                Long storeAreaWarehouseRelId = inventory.getStoreAreaWarehouseRelId();
                StoreAreaWarehouseRel storeAreaWarehouseRel = wareHouseMapper.queryStoreAreaWarehouseRel(storeAreaWarehouseRelId);
                Long storeId = storeAreaWarehouseRel.getStoreId();
                Store store = wareHouseMapper.queryStore(storeId);
                Long inventoryCount = inventory.getInventoryCount();
                store.setProductionCount(inventoryCount);
                int i1 = wareHouseMapper.updateStore(store);
                if (i1 < 0){
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    map.put("code","205");
                    map.put("msg","仓库库存数量保存失败");
                    return map;
                }
                return map;
            }
        }else{
            inventory.setProfitAndLoss(3l);
            int i = wareHouseMapper.addInventory(inventory);
            if (i>0){
                map.put("code","200");
                map.put("msg","盘点不盈不亏");
                return map;
            }
        }
        return map;
    }

    @Override
    public HashMap<String,String> queryInventory(Long  orderId) {
        HashMap<String,String> map = new HashMap<>();
        Inventory inventory = wareHouseMapper.queryInventory(orderId);
        if (inventory != null){
            map.put("code","208");
            map.put("msg","该条已经盘点过,请等下一次盘点时间");
            return map;
        }
        return map;
    }
}
